import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../i18n/locale_keys.dart';
import '../../style/space.dart';
import '../../widget/welcome_slider.dart';
import '../../widget/button.dart';
import '../../widget/slider_indicator.dart';
import 'splash_logic.dart';

class SplashPage extends GetView<SplashLogic> {
  SplashPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder(builder: (_){
         return Scaffold(
           body: SafeArea(
             child: _buildView(),
           ),
         );
    },
    init: SplashLogic(),
    id: "splash",);
  }

  /// 主视图
  Widget _buildView() {
    return <Widget>[
      // slider切换
      _buildSlider(),
      // 控制栏
      _buildBar(),
    ].toColumn(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
    ).paddingAll(AppSpace.page);
  }


  /// 控制栏
  Widget _buildBar() {
    return GetBuilder<SplashLogic>(
      id: "bar",
      init: controller,
      builder: (controller) {
        return controller.isShowStart
            ?
        // 开始
        ButtonWidget.primary(
          LocaleKeys.welcomeStart.tr,
          onTap: controller.onToMain,
        ).tight(
          width: double.infinity,
        ) : <Widget>[
          // 跳过
          ButtonWidget.ghost(
            LocaleKeys.welcomeSkip.tr,
            onTap: controller.onToMain,
          ),
          // 指示标
          SliderIndicatorWidget(
            length: 3,
            currentIndex: controller.currentIndex,
          ),
          // 下一页
          ButtonWidget.ghost(
            LocaleKeys.welcomeNext.tr,
            onTap: controller.onNext,
          ),
        ].toRow(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
        );
      },
    );
  }





  /// 轮播图
  Widget _buildSlider() {
    return GetBuilder<SplashLogic>(
      id: "slider",
      init: controller,
      builder: (controller) => controller.items == null
          ? const SizedBox()
          : WelcomeSliderWidget(
        controller.items!,
        onPageChanged: controller.onPageChanged,
        carouselController: controller.carouselController,
      ),
    );
  }


}
